<template>
  <!-- 猜你喜欢 -->
  <view class="caption">
    <text class="text">猜你喜欢</text>
  </view>
  <view class="guess">
    <navigator
      class="guess-item"
      v-for="item in guessLikeList"
      :key="item.id"
      :url="`/pages/goods/goods?id=${item.id}`"
    >
      <image class="image" mode="aspectFill" :src="item.picture"></image>
      <view class="name"> {{ item.name }}</view>
      <view class="price">
        <text class="small">¥</text>
        <text>{{ item.price }}</text>
      </view>
    </navigator>
  </view>
  <view class="loading-text"> {{ finish ? '没有更多数据了~' : '正在加载...' }}</view>
</template>

<script setup lang="ts">
import { getGuessLikeList } from '@/services/home'
import type { GoodsItem, PageParams } from '@/types/global'
import { onMounted, ref } from 'vue'

//强制为必传防止下面 page++ 非空报错
const pageItem: Required<PageParams> = {
  page: 1,
  pageSize: 10,
}
const guessLikeList = ref<GoodsItem[]>([])
const finish = ref<boolean>(false)

const getGuessLike = async () => {
  if (finish.value) {
    uni.showToast({
      title: '没有更多数据了~',
      icon: 'none',
    })
    return
  }

  const { result } = await getGuessLikeList(pageItem)
  guessLikeList.value.push(...result.items)
  if (pageItem.page >= result.pages) {
    finish.value = true
  } else {
    pageItem.page++
  }
}

const resetData = () => {
  guessLikeList.value = []
  pageItem.page = 1
  finish.value = false
}

onMounted(() => {
  getGuessLike()
})

//暴露方法  getMoreData 取别名
defineExpose({
  getMoreData: getGuessLike,
  clearData: resetData,
})
</script>

<style lang="scss">
@import '@/components/styles/XtxGuess.scss';
</style>
